﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using AutoMapper;
using DTO;

namespace DAO
{
    public class DoanhThuThang_DAO
    {
        public static DoanhThuThang_DTO CreateDTO(DoanhThuThang thang)
        {
            DoanhThuThang_DTO DTO = new DoanhThuThang_DTO();
            Mapper.CreateMap<DoanhThuThang, DoanhThuThang_DTO>();
            DTO = Mapper.Map<DoanhThuThang, DoanhThuThang_DTO>(thang);
            return DTO;
        }

        public List<DoanhThuThang_DTO> GetDoanhThuThangList()
        {
            List<DoanhThuThang_DTO> list = new List<DoanhThuThang_DTO>();
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                var query = from thang in context.DoanhThuThangs select thang;
                foreach (var cb in query)
                {
                    DoanhThuThang_DTO DTO = CreateDTO(cb);
                    list.Add(DTO);
                }
            }
            catch (System.Exception ex)
            {
                string str = ex.Message;
            }
            return list;
        }

        public List<DoanhThuThang_DTO> GetDoanhThuThangTheoNamList(string MaDoanhThuNam)
        {
            List<DoanhThuThang_DTO> list = new List<DoanhThuThang_DTO>();
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                var query = from thang in context.DoanhThuThangs 
                                 where thang.MaDoanhThuNam == MaDoanhThuNam
                                 select thang;
                int STT = 1;
                foreach (var cb in query)
                {
                    DoanhThuThang_DTO DTO = CreateDTO(cb);
                    DTO.STT = STT;
                    STT++;
                    list.Add(DTO);
                }
            }
            catch (System.Exception ex)
            {
                string str = ex.Message;
            }
            return list;
        }

        public DoanhThuThang_DTO GetDoanhThuThangDetails(int Thang, string MaNam)
        {
            DoanhThuThang_DTO DTO = new DoanhThuThang_DTO();
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                var query = from cb in context.DoanhThuThangs
                            where cb.Thang == Thang && cb.MaDoanhThuNam == MaNam
                            select cb;
                DoanhThuThang EF = query.First();
                DTO = CreateDTO(EF);
            }
            catch (System.Exception)
            { }
            return DTO;
        }

        public static string MaDoanhThuThangMax()
        {
            QLCBDataContext context = new QLCBDataContext();
            string query = (from cb in context.DoanhThuThangs select cb.MaDoanhThuThang).Max();
            return query;
        }

        public static bool KiemTra(int Thang, string MaNam)
        {
            QLCBDataContext context = new QLCBDataContext();
            var query = from cb in context.DoanhThuThangs
                        where cb.Thang == Thang && cb.MaDoanhThuNam == MaNam
                        select cb;
            if (query.Count() == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public void InsertDoanhThuThang(DoanhThuThang_DTO DTO)
        {
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                DoanhThuThang thang = new DoanhThuThang();
                thang.MaDoanhThuThang = DTO.MaDoanhThuThang;
                thang.Thang = DTO.Thang;
                thang.MaDoanhThuNam = DTO.MaDoanhThuNam;
                thang.SoChuyenBay = DTO.SoChuyenBay;
                thang.TyLe = DTO.TyLe;
                thang.TongDoanhThuThang = DTO.TongDoanhThuThang;
                context.DoanhThuThangs.InsertOnSubmit(thang);
                context.SubmitChanges();
            }
            catch
            { }
        }

        public void UpdateDoanhThuThang(DoanhThuThang_DTO DTO)
        {
            try
            {
                QLCBDataContext context = new QLCBDataContext();
                DoanhThuThang thang = new DoanhThuThang();
                thang = context.DoanhThuThangs.Single(cb => cb.MaDoanhThuThang == DTO.MaDoanhThuThang);
                thang.MaDoanhThuThang = DTO.MaDoanhThuThang;
                thang.Thang = DTO.Thang;
                thang.MaDoanhThuNam = DTO.MaDoanhThuNam;
                thang.SoChuyenBay = DTO.SoChuyenBay;
                thang.TyLe = DTO.TyLe;
                thang.TongDoanhThuThang = DTO.TongDoanhThuThang;
                context.SubmitChanges();
            }
            catch
            { }
        }
    }
}
